<template>
  <el-dialog
    :title="!dataForm.osmId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="usmId">
      <el-input v-model="dataForm.usmId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="psmId">
      <el-input v-model="dataForm.psmId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="psmName">
      <el-input v-model="dataForm.psmName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="psmPrice">
      <el-input v-model="dataForm.psmPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="购买个数" prop="psmBuyCount">
      <el-input v-model="dataForm.psmBuyCount" placeholder="购买个数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="osmCreatTime">
      <el-input v-model="dataForm.osmCreatTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="osmCode">
      <el-input v-model="dataForm.osmCode" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="收货地址" prop="osmDeliveryId">
      <el-input v-model="dataForm.osmDeliveryId" placeholder="收货地址"></el-input>
    </el-form-item>
    <el-form-item label="商品总价" prop="osmTotalPrice">
      <el-input v-model="dataForm.osmTotalPrice" placeholder="商品总价"></el-input>
    </el-form-item>
    <el-form-item label="收货人姓名" prop="osmName">
      <el-input v-model="dataForm.osmName" placeholder="收货人姓名"></el-input>
    </el-form-item>
    <el-form-item label="收货人手机号" prop="osmPhone">
      <el-input v-model="dataForm.osmPhone" placeholder="收货人手机号"></el-input>
    </el-form-item>
    <el-form-item label="支付状态" prop="osmPayStatus">
      <el-input v-model="dataForm.osmPayStatus" placeholder="支付状态"></el-input>
    </el-form-item>
    <el-form-item label="拼团活动id" prop="gsmGroupId">
      <el-input v-model="dataForm.gsmGroupId" placeholder="拼团活动id"></el-input>
    </el-form-item>
    <el-form-item label="砍价活动id" prop="gsmBargainId">
      <el-input v-model="dataForm.gsmBargainId" placeholder="砍价活动id"></el-input>
    </el-form-item>
    <el-form-item label="秒杀活动id" prop="gsmSeckillId">
      <el-input v-model="dataForm.gsmSeckillId" placeholder="秒杀活动id"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="psmImg">
      <el-input v-model="dataForm.psmImg" placeholder="商品图片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          osmId: 0,
          usmId: '',
          psmId: '',
          psmName: '',
          psmPrice: '',
          psmBuyCount: '',
          osmCreatTime: '',
          osmCode: '',
          osmDeliveryId: '',
          osmTotalPrice: '',
          osmName: '',
          osmPhone: '',
          osmPayStatus: '',
          gsmGroupId: '',
          gsmBargainId: '',
          gsmSeckillId: '',
          psmImg: ''
        },
        dataRule: {
          usmId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          psmId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          psmName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          psmPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          psmBuyCount: [
            { required: true, message: '购买个数不能为空', trigger: 'blur' }
          ],
          osmCreatTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          osmCode: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          osmDeliveryId: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' }
          ],
          osmTotalPrice: [
            { required: true, message: '商品总价不能为空', trigger: 'blur' }
          ],
          osmName: [
            { required: true, message: '收货人姓名不能为空', trigger: 'blur' }
          ],
          osmPhone: [
            { required: true, message: '收货人手机号不能为空', trigger: 'blur' }
          ],
          osmPayStatus: [
            { required: true, message: '支付状态不能为空', trigger: 'blur' }
          ],
          gsmGroupId: [
            { required: true, message: '拼团活动id不能为空', trigger: 'blur' }
          ],
          gsmBargainId: [
            { required: true, message: '砍价活动id不能为空', trigger: 'blur' }
          ],
          gsmSeckillId: [
            { required: true, message: '秒杀活动id不能为空', trigger: 'blur' }
          ],
          psmImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.osmId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.osmId) {
            this.$http({
              url: this.$http.adornUrl(`/order/goodsorder/info/${this.dataForm.osmId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.usmId = data.goodsOrder.usmId
                this.dataForm.psmId = data.goodsOrder.psmId
                this.dataForm.psmName = data.goodsOrder.psmName
                this.dataForm.psmPrice = data.goodsOrder.psmPrice
                this.dataForm.psmBuyCount = data.goodsOrder.psmBuyCount
                this.dataForm.osmCreatTime = data.goodsOrder.osmCreatTime
                this.dataForm.osmCode = data.goodsOrder.osmCode
                this.dataForm.osmDeliveryId = data.goodsOrder.osmDeliveryId
                this.dataForm.osmTotalPrice = data.goodsOrder.osmTotalPrice
                this.dataForm.osmName = data.goodsOrder.osmName
                this.dataForm.osmPhone = data.goodsOrder.osmPhone
                this.dataForm.osmPayStatus = data.goodsOrder.osmPayStatus
                this.dataForm.gsmGroupId = data.goodsOrder.gsmGroupId
                this.dataForm.gsmBargainId = data.goodsOrder.gsmBargainId
                this.dataForm.gsmSeckillId = data.goodsOrder.gsmSeckillId
                this.dataForm.psmImg = data.goodsOrder.psmImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/goodsorder/${!this.dataForm.osmId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'osmId': this.dataForm.osmId || undefined,
                'usmId': this.dataForm.usmId,
                'psmId': this.dataForm.psmId,
                'psmName': this.dataForm.psmName,
                'psmPrice': this.dataForm.psmPrice,
                'psmBuyCount': this.dataForm.psmBuyCount,
                'osmCreatTime': this.dataForm.osmCreatTime,
                'osmCode': this.dataForm.osmCode,
                'osmDeliveryId': this.dataForm.osmDeliveryId,
                'osmTotalPrice': this.dataForm.osmTotalPrice,
                'osmName': this.dataForm.osmName,
                'osmPhone': this.dataForm.osmPhone,
                'osmPayStatus': this.dataForm.osmPayStatus,
                'gsmGroupId': this.dataForm.gsmGroupId,
                'gsmBargainId': this.dataForm.gsmBargainId,
                'gsmSeckillId': this.dataForm.gsmSeckillId,
                'psmImg': this.dataForm.psmImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
